<template>
  <div>
    <h1>器械申报</h1>
    <el-card>
      <el-form :model="applyForm" label-width="120px" :rules="rules" ref="applyFormRef">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input-number v-model="applyForm.deviceId" :min="1" />
        </el-form-item>
        <el-form-item label="使用时间" prop="startTime">
          <el-date-picker
            v-model="applyForm.startTime"
            type="datetime"
            placeholder="选择使用时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="预计归还时间" prop="endTime">
          <el-date-picker
            v-model="applyForm.endTime"
            type="datetime"
            placeholder="选择归还时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitApply">提交申请</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { add } from '@/api/applyrequest'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const applyForm = ref({
  deviceId: null,
  startTime: '',
  endTime: ''
})

const rules = {
  deviceId: [{ required: true, message: '请输入设备ID', trigger: 'blur' }],
  startTime: [{ required: true, message: '请选择使用时间', trigger: 'change' }],
  endTime: [{ required: true, message: '请选择归还时间', trigger: 'change' }]
}

const applyFormRef = ref(null)

const submitApply = () => {
  applyFormRef.value.validate(valid => {
    if (valid) {
      const formData = {
        ...applyForm.value,
        userId: userStore.userId
      }
      add(formData).then(() => {
        ElMessage.success('申请提交成功')
        resetForm()
      })
    }
  })
}

const resetForm = () => {
  applyFormRef.value.resetFields()
}
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}
.el-form {
  width: 600px;
}
</style>